<template>
  <div class="box">
    <!-- 1.0 登录界面的绘制 -->
    <div class="login-inner">
      <h3>火星外卖管理系统</h3>

      <el-form
        :model="loginForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item prop="uname">
          <el-input
            v-model="loginForm.uname"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            :type="hiddenPass ? 'password' : 'text'"
            v-model="loginForm.pass"
            prefix-icon="iconfont icon-mima"
            autocomplete="off"
          >
            <template #suffix>
              <i
                class="iconfont pass-icon"
                :class="hiddenPass ? 'icon-eyes' : 'icon-eyes-'"
                @click="hiddenPass = !hiddenPass"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Login } from '@/api/users.js'
import local from '@/utils/local.js'

export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      // 1.0 数据控制
      loginForm: {
        pass: '',
        uname: ''
      },
      // 2.0 规则控制 (规则里面的字段和数据里面应该保持一致)
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        // validator 校验器
        uname: [{ validator: checkName, trigger: 'blur' }]
      },
      // 显示和隐藏密码
      hiddenPass: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 代表前端的请求验证通过
          const { code, msg, token } = await Login({
            account: this.loginForm.uname,
            password: this.loginForm.pass
          })
          // console.log(data)
          if (code === 0) {
            this.$message({
              message: msg,
              type: 'success'
            })

            local.set('token', token)
            // 模拟网络加载的效果
            setTimeout(() => {
              this.$router.push('/')
            }, 1500)
          } else {
            this.$message.error(msg)
          }
        } else {
          this.$message.error('你咋啥都不填呢！！！')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.box {
  height: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-inner {
    width: 340px;
    height: 240px;

    h3 {
      text-align: center;
      color: #fff;
      font-size: 18px;
      letter-spacing: 1.5px;
      margin-bottom: 30px;
      -moz-user-select: none; /*火狐*/
      -webkit-user-select: none; /*webkit浏览器*/
      -ms-user-select: none; /*IE10*/
      -khtml-user-select: none; /*早期浏览器*/
      user-select: none;
    }
  }

  .el-input {
    -moz-user-select: none; /*火狐*/
    -webkit-user-select: none; /*webkit浏览器*/
    -ms-user-select: none; /*IE10*/
    -khtml-user-select: none; /*早期浏览器*/
    user-select: none;
  }

  .el-button {
    width: 100%;
  }

  .pass-icon {
    cursor: pointer;
  }
}
</style>
